package com.bridge.applets.service.impl;

import com.bridge.applets.model.entity.BridgeMeasureModel;
import com.bridge.applets.mapper.BridgeMeasureModelMapper;
import com.bridge.applets.model.po.MeasureModelCountPO;
import com.bridge.applets.model.vo.req.BridgeMeasureReq;
import com.bridge.applets.service.BridgeMeasureModelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bridge.common.enums.YesOrNoEnum;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 桥梁测量样本表 服务实现类
 * </p>
 *
 * @author xyjian
 * @since 2024-11-25
 */
@Service
public class BridgeMeasureModelServiceImpl extends ServiceImpl<BridgeMeasureModelMapper, BridgeMeasureModel> implements BridgeMeasureModelService {

    @Override
    public Integer queryCount(String bridgeCode) {
        return this.lambdaQuery()
                .eq(BridgeMeasureModel::getBridgeCode,bridgeCode)
                .eq(BridgeMeasureModel::getIsDeleted, YesOrNoEnum.NO.getType())
                .count();

    }

    @Override
    public Map<Integer, Integer> queryCountSampleType(String bridgeCode) {
        List<MeasureModelCountPO> measureModelCountPOS = this.baseMapper.queryCountSampleType(bridgeCode);

        return measureModelCountPOS.stream().collect(Collectors.toMap(MeasureModelCountPO::getSampleType,MeasureModelCountPO::getCountNum));
    }

    @Override
    public Map<String, Integer> queryCountByList(List<String> bridgeCodes) {
        if(CollectionUtils.isEmpty(bridgeCodes)){
            return Collections.emptyMap();
        }
        List<MeasureModelCountPO> measureModelCountPOS = this.baseMapper.queryCountByList(bridgeCodes);

        return measureModelCountPOS.stream().collect(Collectors.toMap(MeasureModelCountPO::getBridgeCode,MeasureModelCountPO::getCountNum));
    }

    @Override
    public Map<String, Integer> queryCountGroupCodeTypeByList(List<String> bridgeCodes) {
        if(CollectionUtils.isEmpty(bridgeCodes)){
            return Collections.emptyMap();
        }
        List<MeasureModelCountPO> measureModelCountPOS = this.baseMapper.queryCountGroupCodeTypeByList(bridgeCodes);

        return measureModelCountPOS.stream().collect(Collectors.toMap(this::getKey,MeasureModelCountPO::getCountNum));
    }

    private String getKey(MeasureModelCountPO item){
        return item.getBridgeCode()+"-"+item.getSampleType();
    }

    @Override
    public List<BridgeMeasureModel> queryListAll(BridgeMeasureReq req) {
        return this.lambdaQuery()
                .eq(BridgeMeasureModel::getBridgeCode,req.getBridgeCode())
                .eq(BridgeMeasureModel::getSampleType,req.getSampleType())
                .eq(BridgeMeasureModel::getIsDeleted, YesOrNoEnum.NO.getType())
                .orderByDesc(BridgeMeasureModel::getId)
                .list();
    }

    @Override
    public List<BridgeMeasureModel> queryByIds(List<Long> sampleIds) {
        return this.lambdaQuery()
                .in(BridgeMeasureModel::getId,sampleIds)
                .eq(BridgeMeasureModel::getIsDeleted, YesOrNoEnum.NO.getType())
                .list();
    }

    @Override
    public void deleteByBridgeCode(String bridgeCode) {
        this.lambdaUpdate()
                .set(BridgeMeasureModel::getIsDeleted,YesOrNoEnum.YES.getType())
                .eq(BridgeMeasureModel::getBridgeCode,bridgeCode)
                .eq(BridgeMeasureModel::getIsDeleted,YesOrNoEnum.NO.getType())
                .update();
    }
}
